home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / vb6_sr_1 / convert.frm next >
Text File  |  1999-02-25  |  7KB  |  212 lines

  1. VERSION 5.00
  2. Begin VB.Form Form1 
  3.    Caption         =   "Form1"
  4.    ClientHeight    =   1560
  5.    ClientLeft      =   60
  6.    ClientTop       =   375
  7.    ClientWidth     =   6840
  8.    LinkTopic       =   "Form1"
  9.    ScaleHeight     =   1560
  10.    ScaleWidth      =   6840
  11.    StartUpPosition =   3  'Windows Default
  12.    Begin VB.CommandButton Command1 
  13.       Caption         =   "Convert Numeric Dollar Amount to Text "
  14.       Height          =   375
  15.       Left            =   120
  16.       TabIndex        =   2
  17.       Top             =   1080
  18.       Width           =   6615
  19.    End
  20.    Begin VB.TextBox Text2 
  21.       Height          =   495
  22.       Left            =   1440
  23.       TabIndex        =   1
  24.       Top             =   480
  25.       Width           =   5295
  26.    End
  27.    Begin VB.TextBox Text1 
  28.       Height          =   495
  29.       Left            =   120
  30.       TabIndex        =   0
  31.       Top             =   480
  32.       Width           =   1215
  33.    End
  34.    Begin VB.Label Label1 
  35.       Caption         =   $"Convert.frx":0000
  36.       Height          =   615
  37.       Left            =   120
  38.       TabIndex        =   3
  39.       Top             =   0
  40.       Width           =   6615
  41.    End
  42. End
  43. Attribute VB_Name = "Form1"
  44. Attribute VB_GlobalNameSpace = False
  45. Attribute VB_Creatable = False
  46. Attribute VB_PredeclaredId = True
  47. Attribute VB_Exposed = False
  48. '*****************************************************
  49. 'Project: Convert.VBP
  50. 'Author: Burt Abreu
  51. 'Date: 21 January 1999
  52. '*****************************************************
  53. 'Comments: This code will convert a numeric dollar
  54. 'amount into text for use in such applications as
  55. 'printing a check.
  56. '
  57. 'This project was built using a Tip and code sent to
  58. 'the VB4UandME site by PineryJim@aol.com. It is posted
  59. 'here after the webmaster at VB4UandMe sent me this
  60. 'and a few other code items when he shut down the site.
  61. '-----------------------------------------------------
  62. 'Bug Fixes & Modifications
  63. '-----------------------------------------------------
  64. 'Author     Date      Comments
  65. '-----------------------------------------------------
  66. 'Burt Abreu 01/21/99
  67. '
  68. '*-Converted the original code sample into this project.
  69. '*-Added commenting and renamed some controls and
  70. '  variables.
  71. '*-Removed GoSub and moved ParseChunk routine to sub.
  72. '*-Added validation to insure that a value was entered in
  73. '  the text box.
  74. '*****************************************************
  75. 'You may use this code freely in your own projects
  76. 'with the following conditions:
  77. '
  78. 'No warranty is provided or should be assumed as to
  79. 'suitability of this code for any particular purpose
  80. 'and the author and webmaster of this site have no
  81. 'liablity. All code is provided "AS-IS" -use at your
  82. 'own risk.
  83. '
  84. 'If you wish to post the code to another site or make
  85. 'it a part of any product, freeware or otherwise, whose
  86. 'main feature is the code itself then you must first
  87. 'request permission by contacting Burt Abreu at the
  88. 'Visual Basic Explorer site http://www.vbexplorer.com
  89. '*******************************************************
  90. Option Explicit
  91.  
  92. 'Set up two arrays to hold string values we
  93. 'will use to convert numbers to words
  94. Dim BigOnes(9) As String
  95. Dim SmallOnes(19) As String
  96.    
  97. 'Declare variables
  98. Dim Dollars As String
  99. Dim Cents As String
  100. Dim Words As String
  101. Dim Chunk As String
  102. Dim Digits As Integer
  103. Dim LeftDigit As Integer
  104. Dim RightDigit As Integer
  105.  
  106.  
  107. Public Sub ParseChunk()
  108.     
  109.     Digits = Mid(Chunk, 1, 1)
  110.     If Digits > 0 Then
  111.         Words = Words & " " & SmallOnes(Digits) & " Hundred"
  112.     End If
  113.     Digits = Mid(Chunk, 2, 2)
  114.     If Digits > 19 Then
  115.         LeftDigit = Mid(Chunk, 2, 1)
  116.         RightDigit = Mid(Chunk, 3, 1)
  117.         Words = Words & " " & BigOnes(LeftDigit)
  118.         If RightDigit > 0 Then
  119.             Words = Words & " " & SmallOnes(RightDigit)
  120.         End If
  121.     Else
  122.         If Digits > 0 Then
  123.             Words = Words & " " & SmallOnes(Digits)
  124.         End If
  125.     End If
  126. End Sub
  127.     
  128. Private Sub Command1_Click()
  129.     
  130. 'format the incoming number to guarantee six digits
  131. 'to the left of the decimal point and two to the right
  132. 'and then separate the dollars from the cents
  133.     Text1.Text = Format(Text1.Text, "000000.00")
  134.     Dollars = Left(Text1.Text, 6)
  135.     Cents = Right(Text1.Text, 2)
  136.     
  137.     Words = ""
  138. '------------------------------------------------------------
  139. 'check to make sure incoming number is not too large. Since
  140. 'we are reading in the first 6 characters in the statement
  141. 'above "Dollars = Left(Text1.Text, 6)" this isn't likely but
  142. 'this would be a good place to limit dollar amounts so I left
  143. 'it here. For instance, you might replace the hard coded
  144. '999999 with a variable "MaxDollars" if you wanted to
  145. '------------------------------------------------------------
  146.     If Dollars > 999999 Then
  147.         Text2.Text = "Dollar amount is too large"
  148.     ElseIf Dollars = "" Then
  149.         Text2.Text = "Please enter an amount"
  150.         Exit Sub
  151.     End If
  152.     
  153. 'separate the dollars into chunks
  154.     If Dollars = 0 Then
  155.         Words = "Zero"
  156.     Else
  157.     
  158. 'first do the thousands
  159.         Chunk = Left(Dollars, 3)
  160.         If Chunk > 0 Then
  161.             ParseChunk
  162.             Words = Words & " Thousand"
  163.         End If
  164.         
  165. 'do the rest of the dollars
  166.         Chunk = Right(Dollars, 3)
  167.         If Chunk > 0 Then
  168.             ParseChunk
  169.         End If
  170.     End If
  171.     
  172. 'concatenate the cents and display
  173.     If Cents = 0 Then Cents = "No"
  174.     Words = Words & " and " & Cents & "/100"
  175.     Text2.Text = Words
  176.     Exit Sub
  177.     
  178. End Sub
  179.  
  180. Private Sub Form_Load()
  181. 'Populate the arrays
  182.     BigOnes(1) = "Ten"
  183.     BigOnes(2) = "Twenty"
  184.     BigOnes(3) = "Thirty"
  185.     BigOnes(4) = "Forty"
  186.     BigOnes(5) = "Fifty"
  187.     BigOnes(6) = "Sixty"
  188.     BigOnes(7) = "Seventy"
  189.     BigOnes(8) = "Eighty"
  190.     BigOnes(9) = "Ninety"
  191.     
  192.     SmallOnes(1) = "One"
  193.     SmallOnes(2) = "Two"
  194.     SmallOnes(3) = "Three"
  195.     SmallOnes(4) = "Four"
  196.     SmallOnes(5) = "Five"
  197.     SmallOnes(6) = "Six"
  198.     SmallOnes(7) = "Seven"
  199.     SmallOnes(8) = "Eight"
  200.     SmallOnes(9) = "Nine"
  201.     SmallOnes(10) = "Ten"
  202.     SmallOnes(11) = "Eleven"
  203.     SmallOnes(12) = "Twelve"
  204.     SmallOnes(13) = "Thirteen"
  205.     SmallOnes(14) = "Fourteen"
  206.     SmallOnes(15) = "Fifteen"
  207.     SmallOnes(16) = "Sixteen"
  208.     SmallOnes(17) = "Seventeen"
  209.     SmallOnes(18) = "Eighteen"
  210.     SmallOnes(19) = "Nineteen"
  211. End Sub
  212.